package com.tianma315.admin.material.controller;


import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Calendar;

import com.tianma315.admin.common.base.BaseController;
import com.tianma315.admin.common.utils.ResponseUtil;
import com.tianma315.admin.common.utils.Result;
import com.tianma315.admin.material.VO.PackingMaterialVO;
import com.tianma315.admin.material.VO.PurchaseVO;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tianma315.admin.material.domain.PackingMaterialDO;
import com.tianma315.admin.material.service.PackingMaterialService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;


/**
 * 
 * <pre>
 * 包装材料入库记录
 * </pre>
 * <small> 2019-12-23 15:12:33 | Aron</small>
 */
@Controller
@RequestMapping("/material/packingMaterial")
public class PackingMaterialController extends BaseController {
	@Autowired
	private PackingMaterialService packingMaterialService;
	
	@GetMapping()
	@RequiresPermissions("material:packingMaterial:packingMaterial")
	String PackingMaterial(){
	    return "material/packingMaterial/packingMaterial";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("material:packingMaterial:packingMaterial")
	public Result<Page<PackingMaterialVO>> list(Integer pageNumber, Integer pageSize, PackingMaterialDO packingMaterialDTO){
		// 查询列表数据
//        Page<PackingMaterialDO> page = new Page<>(pageNumber, pageSize);
//
//        Wrapper<PackingMaterialDO> wrapper = new EntityWrapper<PackingMaterialDO>(packingMaterialDTO);
//        page = packingMaterialService.selectPage(page, wrapper);
//        int total = packingMaterialService.selectCount(wrapper);
//        page.setTotal(total);
//        return Result.ok(page);
        Page<PackingMaterialVO> page = packingMaterialService.getPackingMaterialVOPage(pageNumber,pageSize,packingMaterialDTO);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("material:packingMaterial:add")
	String add(){
	    return "material/packingMaterial/add";
	}

	@GetMapping("/edit/{packingId}")
	@RequiresPermissions("material:packingMaterial:edit")
	String edit(@PathVariable("packingId") Integer packingId,Model model){
		PackingMaterialDO packingMaterial = packingMaterialService.selectById(packingId);
		model.addAttribute("packingMaterial", packingMaterial);
	    return "material/packingMaterial/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("material:packingMaterial:add")
	public Result<String> save( PackingMaterialDO packingMaterial){
	    packingMaterial.setState(0);
	    packingMaterial.setCreateDate(Calendar.getInstance().getTime());
	    packingMaterial.setCreateBy(getUserId());
		if(packingMaterialService.insert(packingMaterial)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("material:packingMaterial:edit")
	public Result<String>  update( PackingMaterialDO packingMaterial){
        packingMaterial.setCreateBy(getUserId());
        packingMaterial.setCreateDate(Calendar.getInstance().getTime());
		packingMaterialService.updateById(packingMaterial);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("material:packingMaterial:remove")
	public Result<String>  remove( Integer packingId){
		if(packingMaterialService.deletePackingMaterialById(packingId)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("material:packingMaterial:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Integer[] packingIds){

        for (int i = 0; i < packingIds.length; i++) {
            packingMaterialService.deletePackingMaterialById(packingIds[i]);
        }
        return Result.ok();
	}

	/**
	 * 导出模板
	 */
	@GetMapping( "/exportPackingMaterial")
	public void exportPurchase(HttpServletResponse response){
		String fileName = "包装材料入库导入模板.xls";
		try {
			response = ResponseUtil.getResponse(response,fileName);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		HSSFWorkbook wb =packingMaterialService.exportPackingMaterial();
		try(OutputStream os = response.getOutputStream()) {
			wb.write(os);
			os.flush();
			os.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 进入导入excel
	 */
	@GetMapping( "/packingMaterialImport")
	public String packingMaterialImport(MultipartFile file){
		return "material/packingMaterial/packingMaterialImport";
	}

	/**
	 * 导入excel
	 */
	@ResponseBody
	@PostMapping( "/importPackingMaterial")
	public Result importPackingMaterial(MultipartFile file){
		if (!file.isEmpty()){
			packingMaterialService.importPackingMaterialImport(file,getUserId());
		}
		return Result.ok();
	}


}
